package org.robert.export_client.excel.builder;

import lombok.EqualsAndHashCode;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.CellStyle;
import org.robert.export_client.excel.entity.CellStyleEntity;
import org.robert.export_client.excel.entity.WriteSheet;
import org.robert.export_client.excel.metadata.IBaseQuery;
import org.robert.export_client.excel.metadata.SourceSheetBuilder;
import org.robert.export_client.excel.util.ExcelUtil;

import java.util.Map;

/**
 * @Description
 * @Author robert
 * @Date 2024/1/20 20:52
 **/
@EqualsAndHashCode(callSuper = true)
public class ExcelSheetSourcePathBuilder<T> extends SourceSheetBuilder {

    private WorkbookDataPathBuilder workbookBuilder;

    public ExcelSheetSourcePathBuilder(WorkbookDataPathBuilder workbookBuilder, HSSFSheet sheet, Class<?> clazz,
                                       Map<String, Map<String, String>> dictMap, Integer limit ) throws Exception {
        super(workbookBuilder, sheet, clazz, dictMap, limit);
        this.workbookBuilder = workbookBuilder;
    }

    public ExcelSheetSourcePathBuilder(WorkbookDataPathBuilder workbookBuilder, HSSFSheet sheet, Class<?> clazz,
                                       Map<String, Map<String, String>> dictMap, Integer limit, CellStyleEntity titleStyle) throws Exception {
        super(workbookBuilder, sheet, clazz, dictMap, limit, titleStyle);
        this.workbookBuilder = workbookBuilder;
    }

    @Override
    public WorkbookDataPathBuilder getWorkbookBuilder() {
        return workbookBuilder;
    }

    public void setWorkbookBuilder(WorkbookDataPathBuilder workbookBuilder) {
        this.workbookBuilder = workbookBuilder;
    }

    @Override
    public void doWrite(IBaseQuery repository, Object t) throws Exception {
        super.doWrite(repository, t);
    }

    @Override
    public WriteSheet build(){
        this.flag = false;
        return new WriteSheet(this);
    }
    @Override
    public void finish(){
        super.finish();
        String fileName = this.getWorkbookBuilder().getFileName();
        String fileNameStr = this.getWorkbookBuilder().getPath() + fileName + ".xlsx";
        ExcelUtil.writeWorkbook(this.getWorkbookBuilder().getWorkbook(), fileNameStr);
    }
}
